package com.hwy.struts.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.hwy.struts.dao.CustomerDao;
import com.hwy.struts.domain.Customer;
import com.hwy.struts.utils.HibernateUtil;

public class CustomerDaoImpl implements CustomerDao {

	@Override
	// 查询所有的客户
	// 使用Hibernate  HQL获取所有的数据
	public List<Customer> findAll() {
		Session session = HibernateUtil.getCurrentSession();
		Transaction beginTransaction = session.beginTransaction();
		Query createQuery = session.createQuery("from Customer");
		List<Customer> list = createQuery.list();
		beginTransaction.commit();
		return list;
	}

	// 新增客户
	@Override
	public void save(Customer customer) {
		Session session = HibernateUtil.getCurrentSession();
		Transaction beginTransaction = session.beginTransaction();
		session.save(customer);
		beginTransaction.commit();
	}

	@Override
	public void del(Long cust_id) {
		Session session = HibernateUtil.getCurrentSession();
		Transaction beginTransaction = session.beginTransaction();
		Customer customer = session.get(Customer.class, cust_id);
		session.delete(customer);
		beginTransaction.commit();
	}

}
